package cn.yiyuan.auth.service.imp;

import cn.yiyuan.auth.domain.Role;
import cn.yiyuan.auth.dot.RoleDTO;
import cn.yiyuan.auth.mapper.IRoleMapper;
import cn.yiyuan.base.until.PageInfo;
import cn.yiyuan.org.query.DepQuery;
import cn.yiyuan.auth.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * @user m
 * @project system
 * @date 2023-10-28
 * @package cn.yiyuan.org.service.impl
 */
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)

@Service
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private IRoleMapper mapper;
    @Override
    public void insert(Role role) {
        mapper.insert(role);
    }

    @Override
    public void delete(Serializable id) {
        mapper.delete(id);
    }

    @Override
    public void update(Role role) {
        mapper.update(role);
    }

    @Override
    public List<Role> getAll() {
        return mapper.getAll();
    }

    @Override
    public Role getById(Serializable id) {
        return mapper.getById(id);
    }

    @Override
    public PageInfo<Role> queryPage(DepQuery depQuery) {
        // 查询总条数
//        Long total = mapper.queryPage(depQuery);
        Long total = mapper.queryTotal(depQuery);
        // 查询分页数据
        if (total != 0){
            List<Role> list = mapper.queryPage(depQuery);
            return new PageInfo<Role>(total,list);
        }
        return new PageInfo<Role>();
    }

    @Override
    public void roleAuth(RoleDTO roleDTO) {
        // 清除原来的权限
        mapper.deleteAll(roleDTO);
        // 授权数据入库
        mapper.roleAuth(roleDTO);
    }

    @Override
    public List<Long> queryRolePermissionIds(Long id) {
        return mapper.queryRolePermissionIds(id);
    }

    @Override
    public void batchDelete(List<Long> ids) {
        mapper.batchDelete(ids);
    }
}
